/*###########################################################
# © 2013-2016 Daniel 'grindhold' Brendle
#
# This file is part of Nostril.
#
# Nostril is free software: you can redistribute it and/or 
# modify it under the terms of the GNU Affero General Public License 
# as published by the Free Software Foundation, either 
# version 3 of the License, or (at your option) any later 
# version.
#
# Nostril is distributed in the hope that it will be 
# useful, but WITHOUT ANY WARRANTY; without even the implied 
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
# PURPOSE. See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public 
# License along with Nostril. 
# If not, see http://www.gnu.org/licenses/.
###########################################################*/

using Gee;

namespace nostril {
    namespace data {
        /**
         * This class provides a base for all Objects inside the application that have to be
         * displayed by a GUI element. It provides the necessary methods to allow Gui-Elements
         * to register to them. Those GUI-Elements are called DataRenderers. Everytime the 
         * object changes (by either the signal notify or changed), The DataRenderer's render()-
         *  methods will be called.
         */
        public class Object : GLib.Object {
            private ArrayList<nostril.gui.DataRenderer> renderers = new ArrayList<nostril.gui.DataRenderer>();

            /**
            * The changes-signal
            * This signal complements the notify signal of GLib.Object. It must be called
            * manually everytime, the obejct changes when notify will not be triggered anyway
            */
            public signal void changed();

            private void call_renderers(){
                foreach (nostril.gui.DataRenderer r in this.renderers){
                    r.render();
                }
            }

            /**
            * The constructor connects the necessary signals to
            * the function that calls the assigned DataRenderers
            * @param none
            */
            public Object(){
                this.notify.connect(()=>{
                    this.call_renderers();    
                });

                this.changed.connect(()=>{
                    this.call_renderers();
                });
            }

            /**
            * Adds a DataRenderer to this Object
            */
            public void set_renderer(nostril.gui.DataRenderer f){
                this.renderers.add(f);
            }

            /**
            * Removes a DataRenderer from this Object
            */
            public void unset_renderer(nostril.gui.DataRenderer f){
                this.renderers.remove(f);
            }
        }
    }
}
